<?php


namespace app\reception\controller;


use app\model\Circle;
use app\model\Collection;
use app\model\Commodity;
use app\model\Coupon;
use app\model\Discount;
use app\model\Friend;
use app\model\Grouppurchase;
use app\model\Orders;
use app\model\OrdersCommodity;
use app\model\Shop;
use app\model\Users;

class Assembly {
	//账户信息
	function user_info() {
		$input = input('post.');
		$user = Users::where(['login_id' => $input['login_id']])->select();
		//查询红包总数
		$count = Coupon::where(['users_id' => $user['0']['id']])->select()->count();
		return json(['user' => $user, 'count' => $count]);
	}

	//店家信息及商品信息
	function info() {
		$input = input('post.');
		$shop_info = Shop::where(['id' => 1])->field('name,type,address,district,phone')->select();
		$commodity_info = Commodity::where(['shop_id' => $input['id']])->select();
		return json(['shop_info' => $shop_info, 'commodity_info' => $commodity_info]);
	}

	//总销量排行
	function discount_all() {
		return json(Shop::order('total_sales', 'desc')->select());
	}

	//日销量排行
	function discount_day() {
		$shop = Shop::select();
		foreach ($shop as $v) {
			$sales = Commodity::where(['shop_id' => $v['id']])->max('day_sales');
			$day[] = Commodity::where(['shop_id' => $v['id'], 'day_sales' => $sales])->select();
		}
		return json($day);
	}

	//月销量排行
	function discount_month() {
		$shop = Shop::select();
		foreach ($shop as $v) {
			$sales = Commodity::where(['shop_id' => $v['id']])->max('month_sales');
			$month[] = Commodity::where(['shop_id' => $v['id'], 'month_sales' => $sales])->select();
		}
		return json($month);
	}

	//优惠表信息
	function ranking() {
		return json(Discount::select());
	}

	//团购表
	function grouppurchase() {
		return Grouppurchase::select();
	}

	//好友
	function friend() {
		$input = input('post.');
		try {
			$f = Friend::where(['users_id' => $input])->select();
			foreach ($f as $v) {
				$friend[] = $v->info['0'];
			}
			return json(['state' => true, 'friend' => $friend]);
		} catch (\Exception $e) {
			return json(['state' => false, 'msg' => $e->getMessage()]);
		}
	}

	//添加好友
	function friend_add() {
		$input = input('post.');
		$f = Friend::where(['users_id' => 4])->select();
		if (count($f) == 0) {
			Friend::create(['users_id' => $input['u_id'], 'f_id' => $input['f_id']]);
			return json(['state' => true, 'msg' => '添加成功']);
		}
		if ($f['0']['f_id'] != $input['f_id']) {
			Friend::create(['users_id' => $input['u_id'], 'f_id' => $input['f_id']]);
			return json(['state' => true, 'msg' => '添加成功']);
		} else {
			return json(['state' => false, 'msg' => '该用户已经是您的好友']);
		}
	}

	//吃货圈
	function circle() {
		try {
			$c = Circle::with('op')->select();
			return json(['state' => true, 'circle' => $c]);
		} catch (\Exception $e) {
			return json(['state' => false, 'msg' => $e->getMessage()]);
		}
	}

	//用户领取红包（优惠券）
	function coupon() {
		return Coupon::select();
	}

	//用户订单表
	function orders() {
		$input = input('post.');
		$order= Orders::where(['user_id' => $input['id']])->select();
		return json(['orders'=>$order,]);
	}
	//用户订单商品表
	function order_info() {
		$input = input('post.');
		$order_info= OrdersCommodity::where(['orders_id' => $input['id']])->select();
		return json(['order_info'=>$order_info,]);
	}

	//用户订单添加
	function orders_add() {
		$input = input('post.');
//		$input=['shop_name'=>'aaa','shop_id'=>1,'user_id'=>1,'user_name'=>'aaa','prices'=>50,'info'=>[['commodity_name'=>'aaa','commodity_id'=>1,'number'=>2,'price'=>50],['commodity_name'=>'bbb','commodity_id'=>2,'number'=>2,'price'=>50]]];
		try {
			Orders::insert([
				'shop_name' => $input['shop_name'],
				'shop_id' => $input['shop_id'],
				'user_id' => $input['user_id'],
				'user_name' => $input['user_name'],
				'price' => $input['prices']
			]);
			$id = Orders::where(['shop_id' => $input['shop_id'], 'user_id' => $input['user_id']])->max('id');
			foreach ($input['info'] as $value) {
				OrdersCommodity::insert([
					'commodity_name' => $value['commodity_name'],
					'commodity_id' => $value['commodity_id'],
					'price' => $value['price'],
					'orders_id' => $id,
					'number' => $value['number']
				]);
			}
			return json(['state' => true, 'msg' => '订单创建成功']);
		} catch (\Exception $e) {
			return json(['state' => false, 'msg' => $e->getMessage()]);
		}
	}

	//用户订单状态更新
	function orders_state() {
		$input = input('post.');
		return Orders::update([
			'state' => $input['state']
		], ['id' => $input['id']]);
	}

	//用户订单评价更新
	function orders_comment() {
		$input = input('post.');
		return Orders::update([
			'comment' => $input['comment']
		], ['id' => $input['id']]);
	}

	//用户订单商品详情表
	function orders_commodity() {
		$input = input('post.');
		return OrdersCommodity::where(['orders_id' => $input['id']])->select();
	}

	//用户收藏表
	function collection() {
		$input = input('post.');
		try {
			$u = Collection::where(['users_id' => $input['id']])->select();
			foreach ($u as $v) {
				$collection[] = ($v->info)['0'];
			}
			return json(['state' => true, 'collection' => $collection]);
		} catch (\Exception $e) {
			return json(['state' => false, 'msg' => $e->getMessage()]);
		}
	}
}